package com.ma.chapter4.t8;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName OrderControl
 * @Author: mayongqiang
 * @DATE 2022/3/16 19:31
 * @Description: 同步模式之顺序控制
 */
@Slf4j(topic = "m.OrderControl")
public class OrderControl1 {

    static final Object lock = new Object();
    static boolean t2runned = false;

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized(lock){
                while(!t2runned){
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                log.debug("1");
            }
        }, "t1");
        Thread t2 = new Thread(() -> {
            synchronized(lock){
                log.debug("2");
                t2runned = true;
                lock.notify();
            }
        }, "t2");
        t1.start();
        t2.start();

    }

}
